<?php
/**
 * Created by PhpStorm.
 * User: Egg
 * Date: 2018/3/28
 * Time: 15:34
 */

namespace Admin\Controller;


use Think\Auth;
use Think\Controller\RestController;

class ApiAuthController extends RestController
{
    public function _initialize()
    {
        if (!strpos(ACTION_NAME, 'get')) {
            return;
        }
        $uid = $this->get_user_id();
        $auth = new Auth();
        if (!$auth->check(MODULE_NAME . '/' . ACTION_NAME, $uid)) {
            \Common\Common\print_json_data(403, '', '没有权限访问该模块');
            exit;
        }
    }

    /*
     * 获取用户id
     */
    public function get_user_id($to_login = 'true')
    {
        if (session('?uid')) {
            return session('uid');
        } else {
            if ($to_login) {
                \Common\Common\print_json_data(403, '', '请先登入');
                exit;
            } else {
                return '';
            }
        }
    }
}